package net.system.sys.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.core.base.struts.BaseAction;
import net.core.common.HttpUtil;
import net.system.base.bean.SSysuser;
import net.system.common.HttpUtils;
import net.system.sys.service.IUserService;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

@Scope("request")
@Namespace("/system/user")
@InterceptorRefs(@InterceptorRef("basicStack"))
@ParentPackage("struts-default")
@Results({ @Result(name = "error", location = "/login.jsp") })
public class UserAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	@Autowired
	IUserService service;
	private List<SSysuser> list;
	
	private String recId;
	
	private SSysuser user;
	private Map returnMap;
	
	private String reCode ="";
	
	/**
	 * 用户管理，区分系统管理及下线管理
	 * type=manage：系统管理，referal：下线管理
	 * @return
	 */
	@Action(value = "user", results = { @Result(name = "success", location = "/WEB-INF/pages/system/userList.jsp") })
	public String user() {
		// 管理界面包含查询及列表内容
		HashMap reqMap = new HashMap();
		reqMap = HttpUtil.buildParameterMap(getRequest());
		
		list = service.selectUser(reqMap);
		for(int i=0;i<list.size();i++){
			SSysuser user = list.get(i);
			System.out.println(user.toString());
		}
		return SUCCESS;
	}
	
	@Action(value = "add", results = { @Result(name = "success", location = "/WEB-INF/pages/system/UserAdd.jsp") })
	public String add() {
		// 添加页面
		return SUCCESS;
	}
	
	@Action(value = "edit", results = { @Result(name = "success", location = "/WEB-INF/pages/system/UserEdt.jsp") })
	public String updUser() {
		// 变更页面
		//如果 recId为空。则取session中的userId
		if(null==recId || "".equals(recId)){
			recId=HttpUtils.getCurrentLoginUserId(getRequest());
		}
		user = service.getUser(recId);
		return SUCCESS;
	}
	
	@Action(value = "editPwd", results = { @Result(name = "success", location = "/WEB-INF/pages/system/UserPwd.jsp") })
	public String editPwd() {
		return SUCCESS;
	}
	
	@Action(value = "updPwd", results = { @Result(name = "success", location = "/WEB-INF/pages/system/UserPwd.jsp") })
	public String updPwd() {
		String result= "";
		HashMap reqMap = new HashMap();
		reqMap = HttpUtil.buildParameterMap(getRequest());
		if(null==recId || "".equals(recId)){
			recId=HttpUtils.getCurrentLoginUserId(getRequest());
			reqMap.put("recId", recId);
		}
		reCode = service.updatePwd(reqMap);
		//returnMap.put("issucc", reCode);
		return SUCCESS;
	}
	/**
	 * @return
	 */
	@Action(value = "save", results = { @Result(name = "success", location = "/WEB-INF/pages/system/UserEdt.jsp") })
	public String save() {
		// 保存信息
		// 保存数据
		HashMap reqMap = new HashMap();
		reqMap = HttpUtil.buildParameterMap(getRequest());
		System.out.println(reqMap.toString());
		//如果 recId为空。则增加操作
		if(null==recId || "".equals(recId)){
			service.insert(reqMap);
		}else{
			service.update(reqMap);
		}
		user = service.getUser(recId);
		return SUCCESS;
	}
	
	@Action(value = "show", results = { @Result(name = "success", location = "/WEB-INF/pages/system/UserEdt.jsp") })
	public String show() {
		// 显示详细页面
		user = service.getUser(recId);
		return SUCCESS;
	}
	
	/**
	 * 好友邀请
	 * @return
	 */
	@Action(value = "invite", results = { @Result(name = "success", location = "/WEB-INF/pages/system/UserInvite.jsp") })
	public String invite() {
		// 显示详细页面
		if(null==recId || "".equals(recId)){
			recId=HttpUtils.getCurrentLoginUserId(getRequest());
//			reqMap.put("recId", recId);
		}
		user = service.getUser(recId);
		return SUCCESS;
	}
	
	
	/**
	 * 密码重置
	 * @return
	 */
	@Action(value = "pwdreset", results = { @Result(name = "success", location = "/WEB-INF/pages/system/UserPwdReset.jsp") })
	public String pwdreset() {
		// 显示详细页面
		user = service.getUser(recId);
		return SUCCESS;
	}

	public List<SSysuser> getList() {
		return list;
	}

	public void setList(List<SSysuser> list) {
		this.list = list;
	}

	public String getRecId() {
		return recId;
	}

	public void setRecId(String recId) {
		this.recId = recId;
	}

	public SSysuser getUser() {
		return user;
	}

	public void setUser(SSysuser user) {
		this.user = user;
	}

	public Map getReturnMap() {
		return returnMap;
	}

	public void setReturnMap(Map returnMap) {
		this.returnMap = returnMap;
	}

	public String getReCode() {
		return reCode;
	}

	public void setReCode(String reCode) {
		this.reCode = reCode;
	}
	
}
